package cn.iocoder.yudao.module.resource.service.audiotext;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextRelation;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextSaveReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.keyword.vo.KeyWordRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.tag.vo.TagRespVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.audiotext.AudioTextDO;
import cn.iocoder.yudao.module.resource.dal.dataobject.keyword.KeyWordDO;
import cn.iocoder.yudao.module.resource.dal.dataobject.tag.TagDO;
import cn.iocoder.yudao.module.resource.dal.mysql.audiotext.AudioTextMapper;
import cn.iocoder.yudao.module.resource.dal.mysql.audiotext.model.QueryAudioTextParam;
import cn.iocoder.yudao.module.resource.dal.mysql.keyword.KeyWordMapper;
import cn.iocoder.yudao.module.resource.dal.mysql.tag.TagMapper;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.resource.enums.ErrorCodeConstants.AUDIO_TEXT_NOT_EXISTS;

/**
 * 文本编辑 Service 实现类
 *
 * @author wgs
 */
@Service
@Validated
public class AudioTextServiceImpl implements AudioTextService {

    @Resource
    private AudioTextMapper audioTextMapper;
    @Resource
    private TagMapper tagMapper;
    @Resource
    private KeyWordMapper keyWordMapper;

    @Override
    public Integer createAudioText(AudioTextSaveReqVO createReqVO) {
        // 插入
        AudioTextDO audioText = this.createAudioTextBean(createReqVO);
        audioTextMapper.insert(audioText);
        // 返回
        return audioText.getId();
    }

    private AudioTextDO createAudioTextBean(AudioTextSaveReqVO createReqVO) {
        AudioTextDO audioText = BeanUtil.copyProperties(createReqVO, AudioTextDO.class, "tagList", "keyworldList", "audioTextRelationList");
        if (createReqVO.getTagList() != null) {
            audioText.setTagList(JSON.toJSONString(createReqVO.getTagList()));
        }
        if (createReqVO.getKeyworldList() != null) {
            audioText.setKeyworldList(JSON.toJSONString(createReqVO.getKeyworldList()));
        }
        if (createReqVO.getAudioTextRelationList() != null) {
            audioText.setRelationContent(JSON.toJSONString(createReqVO.getAudioTextRelationList()));
        }
        return audioText;
    }

    @Override
    public void updateAudioText(AudioTextSaveReqVO updateReqVO) {
        // 校验存在
        validateAudioTextExists(updateReqVO.getId());
        // 更新
        AudioTextDO updateObj = this.createAudioTextBean(updateReqVO);
        audioTextMapper.updateById(updateObj);
    }

    @Override
    public void deleteAudioText(Integer id) {
        // 校验存在
        validateAudioTextExists(id);
        // 删除
        audioTextMapper.deleteById(id);
    }

    private void validateAudioTextExists(Integer id) {
        if (audioTextMapper.selectById(id) == null) {
            throw exception(AUDIO_TEXT_NOT_EXISTS);
        }
    }

    @Override
    public AudioTextRespVO getAudioText(Integer id) {
        AudioTextDO audioTextDO = audioTextMapper.selectById(id);
        AudioTextRespVO audioTextRespVo = this.createAudioTextRespVo(audioTextDO);
        return audioTextRespVo;
    }

    @Override
    public AudioTextRespVO createAudioTextRespVo(AudioTextDO audioTextDO) {
        AudioTextRespVO respVO = BeanUtil.copyProperties(audioTextDO, AudioTextRespVO.class, "tagList", "keyworldList", "audioTextRelationList");
        if (audioTextDO.getTagList() != null) {
            List<Integer> tagList = JSON.parseArray(audioTextDO.getTagList(), Integer.class);
            if (tagList.size() > 0) {
                List<TagDO> tagDOList = tagMapper.selectBatchIds(tagList);
                List<TagRespVO> tagRespVOS = BeanUtils.toBean(tagDOList, TagRespVO.class);
                respVO.setTagList(tagRespVOS);
            }
        }
        if (audioTextDO.getKeyworldList() != null) {
            List<Integer> keyworldList = JSON.parseArray(audioTextDO.getKeyworldList(), Integer.class);
            if (keyworldList.size() > 0) {
                List<KeyWordDO> keyWordDOList = keyWordMapper.selectBatchIds(keyworldList);
                List<KeyWordRespVO> keyWordRespVOList = BeanUtils.toBean(keyWordDOList, KeyWordRespVO.class);
                respVO.setKeyworldList(keyWordRespVOList);
            }
        }
        if (audioTextDO.getRelationContent() != null) {
            List<AudioTextRelation> audioTextRelationList = JSON.parseArray(audioTextDO.getRelationContent(), AudioTextRelation.class);
            for (AudioTextRelation audioTextRelation : audioTextRelationList) {
                if (audioTextRelation.getKeywordIds() != null && audioTextRelation.getKeywordIds().size() > 0) {
                    List<KeyWordDO> keyWordDOList = keyWordMapper.selectBatchIds(audioTextRelation.getKeywordIds());
                    audioTextRelation.setKeyWordRespVOList(BeanUtils.toBean(keyWordDOList, KeyWordRespVO.class));
                }
            }
            respVO.setAudioTextRelationList(audioTextRelationList);
        }
        return respVO;
    }

    @Override
    public PageResult<AudioTextDO> getAudioTextPage(AudioTextPageReqVO pageReqVO) {
        QueryAudioTextParam audioTextParam = BeanUtils.toBean(pageReqVO, QueryAudioTextParam.class);
        // 查找标签及关键字参数
        if (pageReqVO.getTags() != null && pageReqVO.getTags().size() > 0) {
            // String[] tags = pageReqVO.getTags().split(" ");
            //List<TagDO> tagByNames = tagMapper.selectBatchIds(pageReqVO.getTags())
            //List<Integer> tagIds = tagByNames.stream().map(m -> m.getId()).collect(Collectors.toList());
            audioTextParam.setTagList(JSON.toJSONString(pageReqVO.getTags()));
        }
        if (pageReqVO.getKeyworlds() != null && pageReqVO.getKeyworlds().size() > 0) {
            //String[] keyWordList = pageReqVO.getKeyworlds().split(" ");
            //List<KeyWordDO> keyWordDOList = keyWordMapper.selectBatchIds(pageReqVO.getKeyworlds());
            // List<Integer> keyWordIds = keyWordDOList.stream().map(c -> c.getId()).collect(Collectors.toList());
            audioTextParam.setKeyWorldList(JSON.toJSONString(pageReqVO.getKeyworlds()));
        }
        int count = audioTextMapper.selectAudioTextCountByPage(audioTextParam);
        PageResult<AudioTextDO> audioTextDOPageResult = new PageResult<>();
        audioTextDOPageResult.setTotal((long) count);
        if (count > 0) {
            List<AudioTextDO> audioTextDOS = audioTextMapper.selectAudioTextByPage(audioTextParam);
            audioTextDOPageResult.setList(audioTextDOS);
        } else {
            audioTextDOPageResult.setList(new ArrayList<>(1));
        }
        return audioTextDOPageResult;
    }

}